export async function request<T>(url: string, options: RequestInit): Promise<T> {
  try {
    console.log('Starting request to:', url);
    console.log('Request options:', options);
    const response = await fetch(url, {
      ...options,
      headers: {
        'Content-Type': 'application/json',
        ...options.headers
      }
    });
    console.log('Received response with status:', response.status);
    console.log('Response headers:', Array.from(response.headers.entries()));

    if (!response.ok) {
      let errorDetails = await response.text();
      try {
        // 尝试解析JSON格式的错误响应
        const errorJson = JSON.parse(errorDetails);
        errorDetails = JSON.stringify(errorJson, null, 2);
      } catch {}
      throw new Error(`HTTP error! status: ${response.status}, details: ${errorDetails}`);
    }

    return await response.json() as T;
  } catch (error) {
    console.error('Request failed:', error);
    throw error;
  }
}